package cn.edu.scau.dao;

import cn.edu.scau.entity.Customer;

import java.util.List;

import org.apache.ibatis.jdbc.SQL;

public class CustomerSqlProvider {

    public String insertSelective(Customer record) {
        SQL sql = new SQL();
        sql.INSERT_INTO("customer");
        
        if (record.getCustomerid() != null) {
            sql.VALUES("customerID", "#{customerid,jdbcType=INTEGER}");
        }
        
        if (record.getCustomername() != null) {
            sql.VALUES("customerName", "#{customername,jdbcType=VARCHAR}");
        }
        
        if (record.getCustomersex() != null) {
            sql.VALUES("customerSex", "#{customersex,jdbcType=VARCHAR}");
        }
        
        if (record.getCustomerage() != null) {
            sql.VALUES("customerAge", "#{customerage,jdbcType=INTEGER}");
        }
        
        if (record.getCustomertelephone() != null) {
            sql.VALUES("customerTelephone", "#{customertelephone,jdbcType=VARCHAR}");
        }
        
        if (record.getCustomeruseflag() != null) {
            sql.VALUES("customerUseflag", "#{customeruseflag,jdbcType=BIT}");
        }
        
        return sql.toString();
    }

    public String updateByPrimaryKeySelective(Customer record) {
        SQL sql = new SQL();
        sql.UPDATE("customer");
        
        if (record.getCustomername() != null) {
            sql.SET("customerName = #{customername,jdbcType=VARCHAR}");
        }
        
        if (record.getCustomersex() != null) {
            sql.SET("customerSex = #{customersex,jdbcType=VARCHAR}");
        }
        
        if (record.getCustomerage() != null) {
            sql.SET("customerAge = #{customerage,jdbcType=INTEGER}");
        }
        
        if (record.getCustomertelephone() != null) {
            sql.SET("customerTelephone = #{customertelephone,jdbcType=VARCHAR}");
        }
        
        if (record.getCustomeruseflag() != null) {
            sql.SET("customerUseflag = #{customeruseflag,jdbcType=BIT}");
        }
        
        sql.WHERE("customerID = #{customerid,jdbcType=INTEGER}");
        
        return sql.toString();
    }
    
    
    
    /**
     * 根据id或名字模糊查询客户列表
     * @param customerID
     * @param customerName
     * @return
     */
    public String findByIdName(Integer customerID,String customerName){
    	SQL sql = new SQL();
    	sql.SELECT("customerID, customerName, customerSex, customerAge, customerTelephone, customerUseflag");
    	sql.FROM("customer");
    	if(null != customerID) {
    		sql.WHERE("customerID like concat('%',#{customerID},'%')");
    	}
    	if(null != customerName) {
    		sql.WHERE("customerName like concat('%',#{customerName},'%')");
    	}
    	return sql.toString();
    }
}